﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
using System.IO;
using System.Xml.Linq;
using NLog;

namespace WOMPS
{
    public class WebCalls
    {
        private static Logger logger = LogManager.GetCurrentClassLogger(); 
        private static readonly string tvDbKey = "E1CDE1B2CBFC1EC3";

        public static Series GetTVDBSeries(SeriesFile sf)
        {
            if (logger.IsInfoEnabled)
            {
                logger.Info("Start GetTVDBSeries - " + sf.SeriesName);
            }
            Series s = new Series();
            WebRequest request = WebRequest.Create("http://thetvdb.com/api/GetSeries.php?seriesname=" + sf.SeriesName);
            try
            {
                WebResponse response = request.GetResponse();
                using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    XDocument tvdbXML = new XDocument();
                    try
                    {
                        //read the document
                        tvdbXML = XDocument.Parse(sr.ReadToEnd());
                        s.id = Convert.ToInt32(tvdbXML.Root.Element("Series").Elements("id").ToList()[0].Value);
                        s.SeriesId = Convert.ToInt32(tvdbXML.Root.Element("Series").Elements("seriesid").ToList()[0].Value);
                        s.SeriesName = tvdbXML.Root.Element("Series").Elements("SeriesName").ToList()[0].Value;
                        s.Language = tvdbXML.Root.Element("Series").Elements("language").ToList()[0].Value; ;
                        s.Network = tvdbXML.Root.Element("Series").Elements("Network").ToList()[0].Value;
                        s.Overview = tvdbXML.Root.Element("Series").Elements("Overview").ToList()[0].Value; ;
                        s.FirstAired = Convert.ToDateTime(tvdbXML.Root.Element("Series").Elements("FirstAired").ToList()[0].Value);
                        s.Banner = tvdbXML.Root.Element("Series").Elements("banner").ToList()[0].Value;

                    }
                    catch (Exception e)
                    {
                        logger.Error("GetTVDBSeries - Failure to read TVDB XML file", e);
                        if (logger.IsInfoEnabled)
                        {
                            logger.Info("End GetTVDBSeries - Failure to read TVDB XML file");
                        }
                        return null;
                    }   
                }
            }
            catch (WebException we)
            {
                logger.Error("GetTVDBSeries - Failure to get TVDB XML file", we);
                if (logger.IsInfoEnabled)
                {
                    logger.Info("End GetTVDBSeries - Failure to get XML file");
                }
                return null;   
            }
            if (logger.IsInfoEnabled)
            {
                logger.Info("End GetTVDBSeries - SeriesId:" + s.SeriesId.ToString());
            }
            return s;
        }
        public static Episode GetTVDBEpisode(int seriesId, DateTime airDate)
        {
            if (logger.IsInfoEnabled)
            {
                logger.Info("Start GetTVDBEpisode - SeriesId:" + seriesId.ToString() + "; AirDate:" + airDate.ToShortDateString());
            }
            Episode e = new Episode();
            WebRequest request = WebRequest.Create("http://thetvdb.com/api/GetEpisodeByAirDate.php?apikey=" + tvDbKey + "&seriesid=" + seriesId.ToString() + "&airdate=" + airDate.ToShortDateString());
            try
            {
                WebResponse response = request.GetResponse();
                using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    XDocument tvdbXML = new XDocument();
                    try
                    {
                        //read the document
                        tvdbXML = XDocument.Parse(sr.ReadToEnd());
                        e.Id= Convert.ToInt32(tvdbXML.Root.Element("Episode").Elements("id").ToList()[0].Value);
                        e.EpisodeNumber = Convert.ToInt32(tvdbXML.Root.Element("Episode").Elements("EpisodeNumber").ToList()[0].Value);
                        e.SeasonNumber = Convert.ToInt32(tvdbXML.Root.Element("Episode").Elements("SeasonNumber").ToList()[0].Value);
                        e.EpisodeName = tvdbXML.Root.Element("Episode").Elements("EpisodeName").ToList()[0].Value;
                        e.FileName = tvdbXML.Root.Element("Episode").Elements("filename").ToList()[0].Value; ;
                        e.GuestStars = tvdbXML.Root.Element("Episode").Elements("GuestStars").ToList()[0].Value;
                        e.OverView = tvdbXML.Root.Element("Episode").Elements("Overview").ToList()[0].Value; ;
                        e.FirstAired = Convert.ToDateTime(tvdbXML.Root.Element("Episode").Elements("FirstAired").ToList()[0].Value);
                        e.SeriesId = Convert.ToInt32(tvdbXML.Root.Element("Episode").Elements("seriesid").ToList()[0].Value);

                    }
                    catch (Exception ex)
                    {
                        logger.Error("GetTVDBEpisode - Failure to read TVDB XML file", ex);
                        if (logger.IsInfoEnabled)
                        {
                            logger.Info("End GetTVDBEpisode - Failure to read XML file");
                        }
                        return null;
                    }
                }
            }
            catch (WebException we)
            {
                logger.Error("GetTVDBEpisode - Failure to get TVDB XML file for " + seriesId.ToString(), we);
                if (logger.IsInfoEnabled)
                {
                    logger.Info("End GetTVDBEpisode - Failure to get XML file for " + seriesId.ToString());
                }
                return null;
            }
            if (logger.IsInfoEnabled)
            {
                logger.Info("End GetTVDBEpisode - EpisodeName:" + e.EpisodeName);
            }
            return e;
        }
    }
}
